﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EpisodeWizard.App.WizardPages
{
	public partial class WizardPageQuery : CoreUI.WizardPage
	{
		public WizardPageQuery()
		{
			InitializeComponent();
		}

		private void WizardPageQuery_EnterPage()
		{
			CoreUI.ThreadHelpers.StartThreadedAction(new ThreadedQuery(), true);
			Core.Program.GetInstance().OnFileListValuesChanged();
		}


		/// <summary>
		/// Initialize the plugins in a separate thread
		/// So that the UI can still update properly
		/// </summary>
		public class ThreadedQuery : Core.ThreadedAction
		{
			public override void Action()
			{
				OnStart();

				int i = 0;
				foreach (Core.EpisodeFile File in Core.Program.GetInstance().EpisodeFiles)
				{
					if (CancelRequest)
					{
						break;
					}

					if (File.PossibleShows.SelectedValue != null && File.PossibleEpisodes.SelectedValue != null)
					{
						++i;
						int Percentage = i * 100 / Core.Program.GetInstance().EpisodeFiles.Count;
						OnProgress(Percentage, "Querying titles for " + File.PossibleShows.SelectedValue.ToString(), i + "/" + Core.Program.GetInstance().EpisodeFiles.Count);
						File.QueryTitle();
						File.PossibleTitles.AutoSelect();
					}
				}

				OnEnd();
			}

		}



	}
}
